const config = require('../config/mysql.config')
const Knex = require('knex')
const logger = require('logger')

class MySql {
  construction() {
    try {
      /* mysql 数据写入 */
      this.writeMySql = new Knex({ 
        client: 'mysql',
        debug: config.debug,
        connection: {
          host : config.host,
          user : config.user,
          password : config.password,
          database : config.database
        },
        pool: {
          min: config.minConnection,
          max: config.maxConnection
        }
      })
      /* mysql 数据读取 */
      this.readMySql = new Knex({
        client: 'mysql',
        debug: config.debug,
        connection: {
          host : config.host,
          user : config.user,
          password : config.password,
          database : config.database
        },
        pool: {
          min: config.minConnection,
          max: config.maxConnection
        }
      })
    } catch (error) {
      // db创建出现异常，需要进行告警
      logger.getLogger('error').error("创建数据库链接错误", e.message);
      console.log("创建数据库链接错误:", e);
    }
  }
}
module.exports = MySql